<template>
  <div class="app-container">

    <el-breadcrumb>
      <el-breadcrumb-item :to="{path:'/index'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>日志管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :inline="true" ref="queryForm" :model="query">
      <el-form-item label="IP" prop="ip">
        <el-input clearable v-model="query.ip" size="small" placeholder="IP"></el-input>
      </el-form-item>
      <el-form-item label="URL" prop="url">
        <el-input clearable v-model="query.url" size="small" placeholder="URL"></el-input>
      </el-form-item>
      <el-form-item label="操作用户" prop="username">
        <el-input clearable v-model="query.username" size="small" placeholder="操作用户"></el-input>
      </el-form-item>
      <el-form-item label="操作时间">
        <el-date-picker
            v-model="query.operateTimeStart"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime" size="small"
            placeholder="选择开始时间">
        </el-date-picker>
        -
        <el-date-picker
            v-model="query.operateTimeEnd"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime" size="small"
            placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">查询</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border>
      <el-table-column type="selection" width="50"/>
      <el-table-column label="序号">
        <template #default="scope">{{ (query.currentPage - 1) * (query.pageSize) + scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column prop="realName" label="操作用户"></el-table-column>
      <el-table-column prop="ip" label="IP"></el-table-column>
      <el-table-column prop="url" label="URL"></el-table-column>
      <el-table-column prop="operateTime" label="操作时间"></el-table-column>
      <el-table-column label="操作" fixed="right" width="200">
        <template #default="scope">
          <el-button size="mini" type="primary" @click="showDialog(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        background
        :page-size="query.pageSize"
        :total="total"
        :current-page.sync="query.currentPage"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" @closed="handleClose" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="操作用户 :">
              {{ form.realName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类名 :">
              {{ form.className }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方法名 :">
              {{ form.method }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP :">
              {{ form.ip }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="URL :">
              {{ form.url }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间 :">
              {{ form.operateTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="描述 :">
          {{ form.description }}
        </el-form-item>
        <el-form-item label="参数 :">
          <json-viewer :value="form.param" boxed copyable :expand-depth="2" :expanded="false"></json-viewer>
        </el-form-item>
        <el-form-item label="返回值 :">
          <json-viewer :value="form.result" boxed copyable :expand-depth="2" :expanded="false"></json-viewer>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="handleClose">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import JsonViewer from 'vue-json-viewer'
import request from "@/http/request";
import qs from "qs";

export default {
  components: {JsonViewer},
  data() {
    return {
      // 查询参数
      query: {
        currentPage: 1,
        pageSize: 5,
        ip: undefined,
        url: undefined,
        username: undefined,
        operateTimeStart: undefined,
        operateTimeEnd: undefined
      },
      // 总记录数
      total: 1,
      // 表格数据
      tableData: [],
      // 选择的ids
      selectedIds: [],
      // 对话框标题
      dialogTitle: '详情',
      // 对话框显示状态
      dialogVisible: false,
      // 表单对象
      form: {
        id: undefined,
        userId: undefined,
        realName: undefined,
        clas: undefined,
        method: undefined,
        ip: undefined,
        url: undefined,
        operateTime: undefined,
        description: undefined,
        param: {},
        result: {}
      },
    };
  },
  methods: {
    listData() {
      // TODO 发送异步请求处理 ...
      /*request({
        url: '/api/sys/log/list',
        method: 'get',
        params: this.query
      }).then(res => {
        this.total = res.data.total;
        this.tableData = res.data.list;
      });*/
      // 以下是模拟服务端响应的用户分页对象
      this.total = 22
      this.tableData = [{
        id: 1,
        userId: 1,
        realName: 'guest',
        className: 'UserController',
        method: 'login',
        ip: '127.0.0.1',
        url: '/user/logn',
        operateTime: '2022-09-12 14:45:30',
        description: '用户登录',
        param: {},
        result: {}
      }]
      window.scrollTo(0, 0)
    },
    handleQuery() {
      this.query.currentPage = 1
      this.listData()
    },
    resetQuery() {
      this.$refs['queryForm'].resetFields()
      this.query.operateTimeStart = undefined
      this.query.operateTimeEnd = undefined
      this.handleQuery()
    },
    handleCurrentChange(val) {
      this.query.currentPage = val
      this.listData()
    },
    handleSizeChange(val) {
      this.query.pageSize = val
      this.listData()
    },
    showDialog(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleClose() {
      this.form = {
        id: undefined,
        userId: undefined,
        realName: undefined,
        clas: undefined,
        method: undefined,
        ip: undefined,
        url: undefined,
        operateTime: undefined,
        description: undefined,
        param: {},
        result: {}
      }
      this.dialogVisible = false
    },
  },
  mounted() {
    this.listData()
  }
}
</script>